<?php

class CardController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/nonelayout';

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index'),
				'users'=>array('*'),
			)
		);
	}

	public function actionIndex()
	{
		$c = Catalog::model()->findAll();
		$array = array();
		if($c){
			foreach($c as $ca){
				$a = array();
				if($ca->cid != 1){
					$a['cid'] = $ca->cid;
					$a['cataname'] = $ca->cataname;
					$array[]=$a;
				}
			}
		}
		
		$model=new UserLogin;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['UserLogin']))
		{
			$model->attributes=$_POST['UserLogin'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->HomeUrl);
		}
		
		
		$reg_model=new UserRegister;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='reg-form')
		{
			echo CActiveForm::validate($reg_model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['UserRegister']))
		{
			$reg_model->attributes=$_POST['UserRegister'];
			// validate user input and redirect to the previous page if valid
			if($reg_model->validate() && $reg_model->regist())
				$this->redirect(Yii::app()->HomeUrl);
		}
		

		
		if(Yii::app()->user->getState('global_user')){
			$global_user = Yii::app()->user->getState('global_user');
		}else{
			$global_user = null;
		}
		
		
		$this->render('index',array('catalog'=>$array,'model'=>$model,'global_user'=>$global_user,'reg_model'=>$reg_model));
	}
	
	public function actionLearn(){
		if(Yii::app()->user->getState('global_user')){
			$global_user = Yii::app()->user->getState('global_user');
			$catalog = empty($_GET['catalog'])?0:$_GET['catalog'];
			$this->render('learn',array('catalog'=>$catalog,'global_user'=>$global_user));
		}else{
			$this->redirect(Yii::app()->homeUrl);
		}
	}
	
	public function actionGetWords(){
		$c = Catalog::model()->count();
		$c =$c - 1;
		
		$cid = $_GET['catalog'];
		$words = Word::model()->with('c','s')->findAll('t.cid=:cid',array(':cid'=>$cid));
		$array = array();
		$catalog = "";
		if($words != null){
			foreach($words as $w){
				$catalog = $w->c->cataname;
				$a = array();
				$a['spell'] = $w->spell;
				$a['phonetic'] = stripslashes($w->phonetic);
				$a['chinese'] = stripslashes($w->chinese);
				$a['speech'] = $w->s->abbreviation;
				$a['sentence'] = stripslashes($w->sentence);
				$array[]=$a;
			}
			echo json_encode(array('success'=>'true','totalcata'=>$c,'catalog'=>$catalog,'words'=>$array));
		}else{
			echo json_encode(array('success'=>'false'));
		}
		
	}
	
	public function actionLogout(){
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}
